/*
 * Created by SharpDevelop.
 * User: user
 * Date: 01/01/2007
 * Time: 12:40 a.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using System.IO;
using System.Text;

namespace MyCodeGeneration
{
	/// <summary>
	/// Description of ClassGenerator.
	/// </summary>
	public class ClassGenerator		
	{			
		private string t_class, t_field, t_property, t_toString, t_region; //agregar t_header para la cabecera de la licencia :D
		private string sp_insert;
		
		
		private string d_field, d_property, d_toString;	// d_  = declaracion
		
		private string field, type, name;
		private StringBuilder class_ ,fields, properties, toString;
		
		private string nombreTabla, fileOutput, namespace_;		
		
		private DataTable estructura;		
		
		private StreamWriter sw;		
		private StreamReader sr;
		
		private string nl = "\n";		
		private string tab = "\t";
		
		#region propiedades		
		public string FileOutput {
			get {
				return fileOutput;
			}
			set {
				fileOutput = value;
			}
		}
		public string NombreTabla {
			get {
				return nombreTabla;
			}
			set {
				nombreTabla = value;
			}
		}		
		
		public DataTable Estructura {
			get {
				return estructura;
			}
			set {
				estructura = value;
			}
		}

		public string Namespace_ {
			get {
				return namespace_;
			}
			set {
				namespace_ = value;
			}
		}
		#endregion			
		
		public ClassGenerator(){
			Init();
		}
		public ClassGenerator(string nombreTabla, DataTable estructura, string fileOuput, string namespace_)	
		{
			this.nombreTabla = nombreTabla;
			this.estructura  = estructura;
			this.fileOutput  = fileOuput;
			this.namespace_  = namespace_;			
			Init();
		}
	
		public void Init(){
			fields		= new StringBuilder();
         properties	= new StringBuilder();
			toString	   = new StringBuilder();
			class_      = new StringBuilder();
			LoadTemplates();
		}
		
		public void GenerarClase(){			
			sw = File.CreateText(fileOutput);						
					   
			class_.Append(t_class);
			class_.Replace("$namespace",namespace_);
			class_.Replace("$nameclass",nombreTabla);														
			
			foreach (DataRow rw in estructura.Rows){
					
				field	= rw["ColumnName"].ToString();
				type 	= rw["DataType"].ToString().Split('.')[1].ToString();

				//Console.WriteLine(Type.GetType(type).ToString());
						
				d_field	= t_field.Replace("$type",type);			   //Declaracion de campo
				d_field	= d_field.Replace("$name",field);
					
				d_property	= t_property.Replace("$type",type);		//Declaracion de Property
				d_property	= d_property.Replace("$field",field);				
						
				name	= field.Insert(0,"_");
				d_property	= d_property.Replace("$name",name);
						
				d_toString	= t_toString.Replace("$field",field);	//Declaracion prop, ToString
				d_toString	= d_toString.Replace("$name","s"+name);

            fields      = fields.Append(d_field);				   //Almacena todos los campos de la clase
            properties  = properties.Append(d_property);       //Almacena todas las properties ....
            toString    = toString.Append(d_toString);         //Metodos toString();                                   
   		}		      
         					
			class_ = class_.Replace("$fields",fields.ToString());
			class_ = class_.Replace("$properties",properties.ToString());
			class_ = class_.Replace("$toString",toString.ToString());					
			
			sw.Write(class_);
			sw.Close();
		}
		/// <summary>
		///   Genera el procedimiento almacenado para la tabla
		/// </summary>
		/// <param name="drw"> Contiene la Estructura de la tabla </param>
		/// <returns>retorna el Procedimiento almacenado </returns>
		private string GetInsertSql(DataRowCollection drw){
		      0		      
		}
		
				
		private void LoadTemplates(){
		
         sr =  File.OpenText("./templates/cs/class.cs");
         t_class = sr.ReadToEnd();
         sr.Close();
		   
			sr =  File.OpenText("./templates/cs/field.cs");			
			t_field = sr.ReadToEnd();
			sr.Close();			
			
			sr = File.OpenText("./templates/cs/property.cs");
			t_property = sr.ReadToEnd();
			sr.Close();			
			
			sr = File.OpenText("./templates/cs/ToString.cs");
			t_toString = sr.ReadToEnd();
			sr.Close();
			
         sr = File.OpenText("./templates/insertProcedure.cs");
         sp_insert = sr.ReadToEnd();
         sr.Close();						
			/*
         sr =  File.OpenText("./templates/cs/region.cs");
         t_class = sr.ReadToEnd();
         sr.Close();*/
		}
					
	}
}
